package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.mfg.DAWorkBeginActivity;
import com.glory.dgcpcpda.activity.mfg.LotShelvesActivity;
import com.glory.dgcpcpda.activity.mfg.MaterielUpActivity;
import com.glory.dgcpcpda.adapter.CpcTeBindEqpInfoAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.TeBindEqpEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CpcTeBindEqpActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etUserame)
    FreeUI_ClearEditText etUserame;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvTeamWork)
    TextView tvTeamWork;
    @BindView(R.id.tvBindDate)
    TextView tvBindDate;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvTeBindInfoList)
    RecyclerView rvTeBindInfoList;

    private CpcTeBindEqpInfoAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_tebindeqp;
    }

    @Override
    protected void initView() {
        initLayout();
        //initEdit(etEqpId, s -> checkBind(s));
        System.out.println("####："+123);

        initEdit(etUserame,s->getUserTeam(s));

        initEdit(etEqpId,s ->isBinded());

    }
    private void initLayout() {
        tvTitle.setText("技术员绑定设备");
        setMainLayout(llMain);
        tvBindDate.setText(new SimpleDateFormat( "yyyy-MM-dd" ).format(new Date()));

        initRecyclerView(rvTeBindInfoList,this);
        mAdapter = new CpcTeBindEqpInfoAdapter(null);
        mAdapter.isFirstOnly(true);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvTeBindInfoList.setAdapter(mAdapter);
    }
    @Override
    protected void initData() {

    }

    protected void requestDat(){

            FreeUI_EntityCallBack<BaseResponseBean<TeBindEqpEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<TeBindEqpEntity>>
                    (new TypeToken<BaseResponseBean<TeBindEqpEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<TeBindEqpEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        List<TeBindEqpEntity> teBindEqpList = response.body().getResponse().getBody().getTEBINDEQPLIST();

                        etUserame.setText("");
                        etUserame.setFocusable(true);
                        etUserame.requestFocus();
                        etUserame.setFocusableInTouchMode(true);
                        etUserame.requestFocusFromTouch();
                        etEqpId.setText("");
                       mAdapter.setNewData(teBindEqpList);
                        showToast("提交成功",true);

                        CpcTeBindEqpActivity.this.finish();
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<TeBindEqpEntity>> response) {
                    super.onError(response);
                    showToast("ERROR",false);

                }
            };
            List<TeBindEqpEntity> list = new ArrayList<>();

            for (TeBindEqpEntity entity : mAdapter.getData()){
                entity.setCREATEDBY(Constant.CURRENT_USER);
                entity.setUPDATEDBY(getText(etUserame));
                list.add(entity);
            }

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "BIND");
            map.add("TEBINDEQPLIST", new JsonParser().parse(new Gson().toJson(list)).getAsJsonArray());
            map.addProperty("OPERATOR", getText(etUserame));
            map.addProperty("BINDATE", tvBindDate.getText().toString());
            NetUtils.requestPostNet("CPC.BIND","TRANSFERMES",this,map,callBack);
        }

        public void checkBind(String eqpId){
            List<TeBindEqpEntity> teBindEqpList = new ArrayList<>();
            TeBindEqpEntity te=new TeBindEqpEntity(getText(etUserame),tvTeamWork.getText().toString(),eqpId,"",tvBindDate.getText().toString(),"","");
            teBindEqpList.add(te);
            mAdapter.setNewData(teBindEqpList);

            requestDat();
        }
        public void getUserTeam(String userName){
            tvTeamWork.setText("");
            FreeUI_EntityCallBack<BaseResponseBean<TeBindEqpEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<TeBindEqpEntity>>
                    (new TypeToken<BaseResponseBean<TeBindEqpEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<TeBindEqpEntity>> response) {
                    super.onSuccess(response);
                    if("FAIL".equals(response.body().getResponse().getHeader().getRESULT())){
                        etUserame.setText("");
                        etUserame.setFocusable(true);
                        etUserame.requestFocus();
                        etUserame.setFocusableInTouchMode(true);
                        etUserame.requestFocusFromTouch();
                    }

                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        mAdapter.setNewData(null);
                        TeBindEqpEntity t=  response.body().getResponse().getBody();
                        List<TeBindEqpEntity> list=t.getTEBINDEQPLIST();
                        tvTeamWork.setText(t.getWORKTEAM());
                        mAdapter.setNewData(list);

                    }
                }
                @Override
                public void onError(Response<BaseResponseBean<TeBindEqpEntity>> response) {
                    super.onError(response);
                    System.out.println("错误结果：");
                    showToast("ERROR",false);

                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "SEARCH");
            map.addProperty("OPERATOR", getText(etUserame));
            map.addProperty("BINDATE", tvBindDate.getText().toString());
            NetUtils.requestPostNet("CPC.BIND","TRANSFERMES",this,map,callBack);
        }
        public void isBinded(){
        if("".equals(getText(etUserame))){
            showToast("请先扫描工号",false);
            etUserame.setText("");
            etUserame.setFocusable(true);
            etUserame.requestFocus();
            return;
        }
            FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<Object>>
                    (new TypeToken<BaseResponseBean<Object>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<Object>> response) {
                    super.onSuccess(response);
                    if("FAIL".equals(response.body().getResponse().getHeader().getRESULT())){
                        etEqpId.setText("");
                        etEqpId.setFocusable(true);
                        etEqpId.requestFocus();
                        etEqpId.setFocusableInTouchMode(true);
                        etEqpId.requestFocusFromTouch();
                    }
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        Map<String,Object> map = (Map<String, Object>) response.body().getResponse().getBody();
                        List<TeBindEqpEntity> teBindEqpList = new ArrayList<>();

                        for (TeBindEqpEntity entity : mAdapter.getData()){
                            if(!entity.getUSERNAME().equals(getText(etUserame))){
                                showToast("请勿同时绑定多个工号",false);
                                return;
                            }else if(entity.getEQPID().equals(getText(etEqpId))&&entity.getBINDATE().equals(tvBindDate.getText().toString())){
                                showToast("该设备已在列表中，请勿重复绑定",false);
                                return;
                            }
                            teBindEqpList.add(entity);
                        }
                        TeBindEqpEntity te=new TeBindEqpEntity(getText(etUserame),tvTeamWork.getText().toString(),getText(etEqpId),"",tvBindDate.getText().toString(),"","");
                        teBindEqpList.add(te);
                        mAdapter.addData(te);
                        etEqpId.setText("");
                        etEqpId.setFocusable(true);
                        etEqpId.requestFocus();
                        etEqpId.setFocusableInTouchMode(true);
                        etEqpId.requestFocusFromTouch();

                    }
                }
                @Override
                public void onError(Response<BaseResponseBean<Object>> response) {
                    super.onError(response);
                    showToast("ERROR",false);

                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "CHECK");
            map.addProperty("OPERATOR", getText(etUserame));
            map.addProperty("EQPID", getText(etEqpId));
            map.addProperty("BINDATE", tvBindDate.getText().toString());
            map.addProperty("CREATEDBY", Constant.CURRENT_USER);
            map.addProperty("UPDATEDBY", getText(etUserame));
            map.addProperty("WORKTEAM", tvTeamWork.getText().toString());
            NetUtils.requestPostNet("CPC.BIND","TRANSFERMES",this,map,callBack);
        }
    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:

                if("".equals(getText(etUserame))){
                    showToast("请先扫描工号",false);
                    etUserame.setText("");
                    etUserame.setFocusable(true);
                    etUserame.requestFocus();
                    return;
                }
                requestDat();
                break;
        }
    }

}
